package com.lib.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author rames
 * @date 2019/3/25 11:58
 */
public class TimeUtil {

    // java中怎样计算两个时间如：“21:57”和“08:20”相差的分钟数、小时数 java计算两个时间差小时 分钟 秒 .
    public static void timeSubtract() {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd");
        Date begin = null;
        Date end = null;
        try {
            begin = dfs.parse("2004-01-02");
            end = dfs.parse("2004-03-26");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒

        long day1 = between / (24 * 3600);
        long hour1 = between % (24 * 3600) / 3600;
        long minute1 = between % 3600 / 60;
        long second1 = between % 60;
        System.out.println("" + day1 + "天" + hour1 + "小时" + minute1 + "分"+ second1 + "秒");
    }

    /**
     * 82s--->1分22秒
     * @param time
     */
    public static String getTimeDistance(int time){
        String t = time+"秒";
        if(time < 60){
            return time+"秒";
        } else if (time >= 60 && time < 60*60){
            return time/60+"分"+time%60+"秒";
        } else if(time >= 3600 && time < 24*60*60){
            return time/(60*60)+"小时"+(time%(60*60))/60+"分"+time%60+"秒";
        }
        return t;
    }
}
